<template>
  <div class="sub-filter" v-if="filterData&&!filterLoading">
    <div class="item">
      <div class="head">品牌: </div>
      <div class="body">
        <!-- <a href="javascript:;">全部</a> -->
         <a href="javascript:;" v-for="brand in filterData.brands" :key="brand.id" :class="{active:filterData.selectedBrand===brand.id}" @click="changeBrand(brand.id)">{{brand.name}}</a>
      </div>
    </div>

    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{p.name}}: </div>
      <div class="body">
        <!-- <a href="javascript:;">全部</a> -->
         <a href="javascript:;" v-for="attr in p.properties" :key="attr.id" :class="{active:p.selectedProp===attr.id}" @click="changeAttr(p,attr.id)">{{attr.name}}</a>
      </div>
    </div>
  </div>

  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>

<script>
import { findSubCategoryFilter } from '@/api/category.js'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',

  setup (props, { emit }) {
    const route = useRoute()

    const filterData = ref(null)

    const filterLoading = ref(false)

    // 获取filter数据

    watch(() => route.params.id, (newVal, oldVal) => {
      console.log('newVal', newVal)
      console.log('router-path', route.path)
      if (newVal && route.path === ('/category/sub/' + newVal)) {
        filterLoading.value = true

        findSubCategoryFilter(route.params.id).then(({ result }) => {
          result.selectedBrand = null
          result.brands.unshift({ id: null, name: '全部' })

          result.saleProperties.forEach(p => {
            p.selectedProp = undefined
            p.properties.unshift({ id: null, name: '全部' })
          })

          filterData.value = result
          filterLoading.value = false
        })
      }
    }, { immediate: true })

    const getFilterParams = () => {
      const filterParams = {}
      const attrs = []
      filterParams.brandId = filterData.value.selectedBrand

      filterData.value.saleProperties.forEach(p => {
        const attr = p.properties.find(attr => attr.id === p.selectedProp)

        if (attr && attr.id !== undefined) {
          attrs.push({ groupName: p.name, propertyName: attr.name })
        }
      })

      if (attrs.length) filterParams.attrs = attrs
      return filterParams
    }

    const changeBrand = (brandId) => {
      if (filterData.value.selectedBrand === brandId) return
      filterData.value.selectedBrand = brandId
      emit('filter-change', getFilterParams())
    }

    const changeAttr = (p, attrId) => {
      if (p.selectedProp === attrId) {
        return
      }

      filterData.value.selectedProp = attrId
      emit('filter-change', getFilterParams())
    }

    return { filterData, filterLoading, changeBrand, changeAttr }
  }

}
</script>

<style lang="less" scoped>
 // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }

      .xtx-skeleton {
      padding: 10px 0;
    }
</style>
